package com.gdut.simple.view.activity.explore;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.gdut.simple.R;
import com.gdut.simple.data.remote.api.NewsApi;
import com.gdut.simple.view.adapter.SimplePageAdapter;
import com.gdut.simple.view.basic.AbsBasicActivity;
import com.gdut.simple.view.fragment.NewsFragment;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class NewsActivity extends AbsBasicActivity {

    @BindView(R.id.id_tab_layout)
    TabLayout mTabLayout;
    @BindView(R.id.id_wrap_view_pager)
    ViewPager mPager;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_news);
        ButterKnife.bind(this);
        initToolbar();
    }

    private void initToolbar() {
        setSupportActionBar(mToolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    finish();
                }
            });
        }
    }

    @Override
    protected void loadData() {
        final List<String> tabs = Arrays.asList(NewsApi.NAMES);
        final List<Fragment> fragments = new ArrayList<>();
        for (String type : NewsApi.TYPES) {
            fragments.add(NewsFragment.newInstance(type));
        }
        final SimplePageAdapter mPagerAdapter = new SimplePageAdapter(getSupportFragmentManager(), fragments, tabs);
        mPager.setOffscreenPageLimit(tabs.size() - 1);
        mPager.setAdapter(mPagerAdapter);
        mTabLayout.setupWithViewPager(mPager);
    }
}
